package com.concurrent.designPattern.runseqenctial;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

// 三个线程顺序打印 1 2 3 4 5 6 7 8 9 10
//思路  线程打印 a 打印  1 4 7 10  ....
//            b 打印 2 5 8  .....
//            c 打印 3 6 9  ....
//TODO
public class Test5 {

    static int count =1;

    public static void main(String[] args) throws InterruptedException {
        ReentrantUtil as = new ReentrantUtil();
        Condition aCon = as.newCondition();
        Condition bCon = as.newCondition();
        Condition cCon = as.newCondition();
        //执行a的
        new Thread(()->{
            as.print(aCon,bCon,1);
        },"t1").start();
        new Thread(()->{
            as.print(bCon,cCon,2);
        },"t2").start();
        new Thread(()->{
            as.print(cCon,aCon,3);
        },"t3").start();
        Thread.sleep(1000);
        as.lock();
        try {
            System.out.println("开始");
            aCon.signalAll();
        }finally {
            as.unlock();
        }
    }
}

class ReentrantUtil extends ReentrantLock{
    public void print(Condition current,Condition next , int printIn){
        while (printIn <= 100) {
            this.lock();
            try {
                current.await();
                System.out.println(printIn++);
                next.signalAll();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                this.unlock();
            }
        }
    }
}